﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication.Components;
using System.Collections.Generic;
using SilverlightApplication.Utils;
using System.Threading;

namespace SilverlightApplication
{
	public partial class MainPage : UserControl
	{
        private const double _originalWidth = 1024;
        private const double _originalHeight = 768;
        private const double _originalAspectRatio = _originalWidth / _originalHeight;
        private Point _mousePoint;

        private bool _btnToggle = false;

        private List<Ballon> _bgBallon;

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width < _originalWidth ||
                e.NewSize.Height < _originalHeight)
            {
                // don't shrink
                PageScale.ScaleX = 1.0;
                PageScale.ScaleY = 1.0;
            }
            else
            {
                // resize keeping aspect ratio the same
                if (e.NewSize.Width / e.NewSize.Height > _originalAspectRatio)
                {
                    // height is our constraining property
                    PageScale.ScaleY = e.NewSize.Height / _originalHeight;
                    PageScale.ScaleX = PageScale.ScaleY;
                }
                else
                {
                    // either width is our constraining property, or the user
                    // managed to nail our aspect ratio perfectly.
                    PageScale.ScaleX = e.NewSize.Width / _originalWidth;
                    PageScale.ScaleY = PageScale.ScaleX;
                }
            }
        }

		public MainPage()
		{
			// Required to initialize variables
            _bgBallon = new List<Ballon>();
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
		}

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            InitBackgound();
            //SizeChanged += new SizeChangedEventHandler(Page_SizeChanged);
            LayoutRoot.MouseMove += new MouseEventHandler(LayoutRoot_MouseMove);
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
        {
            _mousePoint = e.GetPosition(LayoutRoot);
        }

        private void InitBackgound()
        {
            int balloncount = RandomUtils.Random.Next(500);
            for (int i = 0; i < balloncount; i++)
            {
                AddBallon();
            }
        }

        private void TimerAnimeBallon(object state)
        {
            // The state object is the Timer object.
            //Timer t = (Timer)state;
            //t.Dispose();
            //Example.outputBlock.Dispatcher.BeginInvoke(delegate()
            //{
            //    Example.outputBlock.Text += "Timer callback executes.\n";
            //});
            bgCanvas.Dispatcher.BeginInvoke(delegate()
            {
                Ballon ballon = state as Ballon;
                if (ballon.Anime.GetCurrentState() == ClockState.Stopped)
                {
                    ballon.Anime.Begin();
                }
                else
                {
                    ballon.Anime.Resume();
                }
            });
        }

        private Ballon AddBallon()
        {
            Ballon ballon = new Ballon();
            ballon.ParentChanged += new Ballon.ParentChangeHandler(ballon_ParentChanged);
            ballon.CreatwithRandomRadius(10, 100);
            RandomAddtoCanvas(ballon);
            _bgBallon.Add(ballon);

            return ballon;
        }

        private void RandomAddtoCanvas(Ballon ballon)
        {
            ballon.Parent = bgCanvas;
            //int fact = 1;
            //if (RandomUtils.Random.Next(2) == 0)
            //{
            //    fact = -1;
            //}
            //ballon.Left = fact * RandomUtils.Random.Next(480);
            //ballon.Top = -ballon.Left;
            ballon.Left = RandomUtils.Random.Next((int)_originalWidth);
            ballon.Top = RandomUtils.Random.Next((int)_originalHeight);
        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            bgCanvas.RenderTransform.Transform(_mousePoint);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_btnToggle)
            {
                foreach (Ballon ballon in _bgBallon)
                {
                    ballon.Anime.Pause();
                }
                _btnToggle = false;
            }
            else
            {
                foreach (Ballon ballon in _bgBallon)
                {
                    Timer aTimer = new Timer(TimerAnimeBallon, ballon, RandomUtils.Random.Next(3000), RandomUtils.Random.Next(3000));
                }
                _btnToggle = true;
            }
        }

        void ballon_ParentChanged(object sender, Panel oldParent, Panel newParent)
        {
            _bgBallon.Remove(sender as Ballon);

            Ballon ballon = AddBallon();
            ballon.Anime.Begin();
        }
	}
}